package com.linkhub.oms.dao.mpservice.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkhub.oms.common.enums.IEnumConvert;
import com.linkhub.oms.common.enums.mp.TransportTypeEnum;
import com.linkhub.oms.dao.mapper.ThirdPartyLogisticsUrlEntityMapper;
import com.linkhub.oms.dao.mpservice.IThirdPartyLogisticsUrlService;
import com.linkhub.oms.entity.ThirdPartyLogisticsUrlEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 *  * <p>
 *  * 供应商表 服务实现类
 *  * </p>
 * @author Jaycle
 * @version 1.0.0
 * @Description ThirdPartyLogisticsUrlServiceImpl.java
 * @createTime 2024/06/29 22:18
 */
@Service
public class ThirdPartyLogisticsUrlServiceImpl extends ServiceImpl<ThirdPartyLogisticsUrlEntityMapper, ThirdPartyLogisticsUrlEntity> implements IThirdPartyLogisticsUrlService {

    @Resource
    private ThirdPartyLogisticsUrlEntityMapper thirdPartyLogisticsUrlEntityMapper;

    @Override
    public void saveOrUpdateLogisticUrl( String userId, String sysShopId, Integer transportType, String queryUrl ) {
        LocalDateTime now = LocalDateTime.now();

        // 查询是否存在记录
        QueryWrapper<ThirdPartyLogisticsUrlEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("Fuser_id", userId)
                .eq("Fsys_shop_id", sysShopId);
        ThirdPartyLogisticsUrlEntity existingEntity = thirdPartyLogisticsUrlEntityMapper.selectOne(queryWrapper);

        // 准备实体对象
        ThirdPartyLogisticsUrlEntity entity = new ThirdPartyLogisticsUrlEntity();
        entity.setUserId(userId);
        entity.setSysShopId(sysShopId);
        entity.setTransportType(IEnumConvert.valueOf(transportType, TransportTypeEnum.class));
        entity.setQueryUrl(queryUrl);
        entity.setCreatedTime(now);
        entity.setUpdateTime(now);

        if (existingEntity != null) {
            // 如果存在则更新
            entity.setId(existingEntity.getId()); // 设置主键以便更新
            thirdPartyLogisticsUrlEntityMapper.updateById(entity);
        } else {
            // 如果不存在则插入
            thirdPartyLogisticsUrlEntityMapper.insert(entity);
        }
    }

    @Override
    public ThirdPartyLogisticsUrlEntity queryById( String sysShopId ) {
        return lambdaQuery()
//                .eq(ThirdPartyLogisticsUrlEntity::getUserId, userId)
                .eq(ThirdPartyLogisticsUrlEntity::getSysShopId, sysShopId)
                .one();
    }
}
